<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;
use App\Http\model\Dictionary;

class MessageController extends BaseController
{
    /**
     * 消息信息接口
     * by 吉星
     */

    public function text(Request $request){
        $zidian = new Dictionary();  
        $con = $zidian->contents("1,1");  //传入数据表字典字段值
        dump($con);
    }
    // 消息信息数据
    public function message(Request $request){
        if($request){
            $where = [];
            if(isset($request['name'])){
                $where['m.name'] = $request['name'];
            }
            if (isset($request['starttime']) || isset($request['endtime'])) {
                $starttime = strtotime($request['starttime']);
                $endtime = strtotime($request['endtime']);
                $data = Db::table('message as m')
                    ->join('user as u','u.id','=','m.save_id','left')
                    ->where($where)
                    ->whereBetween('m.update_time',[$starttime,$endtime])
                    ->select('m.*','u.name as username')
                    ->get()
                    ->toArray();
            }else{
                $data = Db::table('message as m')
                    ->join('user as u','u.id','=','m.save_id','left')
                    ->where($where)
                    ->select('m.*','u.name as username')
                    ->get()
                    ->toArray();
            }
            $data = json_decode(json_encode($data),true);
            // 获取对应的字典信息
            foreach ($data as $k => $v) {
                $zidian = new Dictionary();
                $value = $zidian->dictionary($v['type']); //传入对应的字段值
                $data[$k]['type'] = $value;  //获取model传来的值,并赋值
            }
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data,'where'=>$where];
            }else{
                $res = ['code'=>1002,'message'=>'没有找到符合条件的数据!','where'=>$where];
            }
        }else{
            $res = ['code'=>1001,'message'=>'参数错误!'];
        }
        
        return $res;
    }
    // 修改消息信息页面赋值
    public function edit(Request $request){
        if($request['id']){
            $data = Db::table('message')->where('id',$request['id'])->first();
            // dump($data);
            $data = json_decode(json_encode($data),true);
            $zidian = new Dictionary();
            $value = $zidian->dictionary($data['type']); //传入对应的字段值
            $data['type'] = $value;  //获取model传来的值,并赋值
            // 模拟字典数据 ->消息类型
            $type = [
                ['name'=>'1','value'=>'消息类型1'],
                ['name'=>'2','value'=>'消息类型2'],
                ['name'=>'3','value'=>'消息类型3']
            ];
            // 模拟字典数据 ->消息来源
            $mfrom =[
                ['name'=>'1','value'=>'消息来源1'],
                ['name'=>'2','value'=>'消息来源2'],
                ['name'=>'3','value'=>'消息来源3']
            ];
            // 模拟字典数据 ->提醒类型
            $remind =[
                ['name'=>'1','value'=>'提醒类型1'],
                ['name'=>'2','value'=>'提醒类型2'],
                ['name'=>'3','value'=>'提醒类型3']
            ];
            // echo "<pre>";
            // print_r($remind);exit;
            return ['data'=>$data,'code'=>1000,'message'=>'获取数据成功','type'=>$type,'mfrom'=>$mfrom,'remind'=>$remind];
        }else{
            return ['code'=>1001,'message'=>'请传入消息配置ID'];
        }
    }
    // 修改消息信息
    public function message_edit(Request $request){
        if($request){
            $dat = $request->all();
            // $data = array_splice($dat,1);
            // $file = $request->file('file');
            if($dat['id']){
                // if($file){
                //     $file_path = 'uploads/' . date("Ymd") . "/";
                //     $extension = $file->getClientOriginalExtension();//上传文件的后缀.
                //     $file_name = md5(time() . mt_rand(10, 99)) . '.' . $extension;
                //     $info = $file->move($file_path, $file_name);
                //     // return $info;
                //     if (!$info) {
                //         $error = $file->getError();
                //         $res = ['code'=>1004,'message'=>$error];
                //     }
                    
                // }
                // $data['sounds'] = $file_path . $file_name;
                if(!empty($dat['name'])){
	            	$data['name'] = $dat['name'];
	            }
	            if(!empty($dat['contents'])){
	            	$data['contents'] = $dat['contents'];
	            }
	            if(!empty($dat['type'])){
	            	$data['type'] = $dat['type'];
	            }
	            if(!empty($dat['m_from'])){
	            	$data['m_from'] = $dat['m_from'];
	            }
	            if(!empty($dat['identity'])){
	            	$data['identity'] = $dat['identity'];
	            }
	            if(!empty($dat['remind'])){
	            	$data['remind'] = $dat['remind'];
	            }
	            if(!empty($dat['sounds'])){
	            	$data['sounds'] = $dat['sounds'];
	            }
                $data['update_time'] = date('Y-m-d H:i;s',time());
            	$data['save_id'] = session('uid');
                $save = Db::table('message')->where('id','=',$dat['id'])->update($data);
                if($save){
                    $res = ['code'=>1000,'message'=>'修改成功!'];
                }else{
                    $res = ['code'=>1001,'message'=>'修改失败,没有数据发生变化!'];
                }
            }else{
                $res = ['code'=>1002,'message'=>'修改失败,没有找到符合条件的数据!'];
            }
        }else{
            $res = ['code'=>1003,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 增加消息信息
    public function message_add(Request $request){
        if($request){
            $dat = $request->all();
            // $data = array_splice($dat,1);
            // dd($dat);
            // unset($dat["s"]);
            // $file = $request->file('file');
            // if($file){
            //     $file_path = 'uploads/' . date("Ymd") . "/";
            //     $extension = $file->getClientOriginalExtension();//上传文件的后缀.
            //     $file_name = md5(time() . mt_rand(10, 99)) . '.' . $extension;
            //     $info = $file->move($file_path, $file_name);
            //     // return $info;
            //     if (!$info) {
            //         $error = $file->getError();
            //         $res = ['code'=>1004,'message'=>$error];
            //     }
            //     $data['sounds'] = $file_path . $file_name;
            // }
            if(!empty($dat['name'])){
            	$data['name'] = $dat['name'];
            }
            if(!empty($dat['contents'])){
            	$data['contents'] = $dat['contents'];
            }
            if(!empty($dat['type'])){
            	$data['type'] = $dat['type'];
            }
            if(!empty($dat['m_from'])){
            	$data['m_from'] = $dat['m_from'];
            }
            if(!empty($dat['identity'])){
            	$data['identity'] = $dat['identity'];
            }
            if(!empty($dat['remind'])){
            	$data['remind'] = $dat['remind'];
            }
            if(!empty($dat['sounds'])){
            	$data['sounds'] = $dat['sounds'];
            }
            
            $data['update_time'] = date('Y-m-d H:i;s',time());
            $data['save_id'] = session('uid');
            // dd($data);
            $save = Db::table('message')->insert($data);
            if($save){
                $res = ['code'=>1000,'message'=>'增加成功!'];
            }else{
                $res = ['code'=>1001,'message'=>'增加失败,没有数据发生变化!'];
            }
        }else{
            $res = ['code'=>1003,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 启用 禁用
    public function del(Request $request){
    	if($request['id']){
    		$data['is_delete'] = $request['is_delete'];
    		$data['update_time'] = date('Y-m-d H:i;s',time());
    		$data['save_id'] = session('uid');
    		$del = Db::table('message')->whereIn('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'操作成功!'];
    		}else{
    			$res = ['code'=>1001,'message'=>'操作失败,数据没有变化!'];
    		}
    	}else{
    		$res = ['code'=>1002,'message'=>'未找到要操作的数据!'];
    	}
    	return $res;
    }
}
